package com.cq;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.cglib.beans.BeanMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;

public class RequestToBean {

	/**
	 * 组装Request参数
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static Map<String, String> assembleRequestParam(HttpServletRequest request) {
		Map<String, String> paraMap = new ConcurrentHashMap<>();
		Enumeration<String> eles = request.getParameterNames();
		while (eles.hasMoreElements()) {
			String key = eles.nextElement();
			if(StringUtils.isNotBlank(key)&&key.indexOf("columns[")<0){
				paraMap.put(key, request.getParameter(key));
			}
		}
		return paraMap;
	}

	public static Map<String, String> assembleUnSelectParam(HttpServletRequest request)
			throws UnsupportedEncodingException {
		Map<String, String> paraMap = new ConcurrentHashMap<>();
		Enumeration<String> eles = request.getParameterNames();
		while (eles.hasMoreElements()) {
			String key = null;
			try {
				key = URLDecoder.decode(eles.nextElement(), "utf-8");
				paraMap.putAll(JSON.parseObject(key, HashMap.class));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (JSONException e) {

			}
		}
		return paraMap;
	}
	
	
	public static <T> T mapToBean(Map<String, Object> map,T bean) {  
	    BeanMap beanMap = BeanMap.create(bean);  
	    beanMap.putAll(map);  
	    return bean;  
	}
	
	
	public static int checkPagesPage(int startInt,int lengthInt){
		if(startInt>=0&&lengthInt>0){
			return (startInt/lengthInt+1);
		}else{
			return 1;
		}
	}
	
}
